import {ref} from 'vue'
//@ts-ignore
import {FetchContext, FetchResponse} from "ofetch";
//@ts-ignore
import {R} from "unplugin-vue-router/types-DBiN4-4c";

// console.log(useRuntimeConfig().public.apiUrl)
console.log(process.env.NODE_ENV)
const fetchConfig = {
    baseURL:process.env.NODE_ENV==='development'?'/qcy-blog':'https://www.qcycloud.com/qcy-blog'
}
export const getUUID = ()=>{
    var s = [];
    var hexDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
    }
    s[14] = "4"
    // @ts-ignore
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
    s[8] = s[13] = s[18] = s[23] = "-"
    let uuid = s.join("")
    return uuid
}
//请求体封装
function useGetFetchOptions(options = {} ){
// @ts-ignore
    options.baseURL = options.baseURL ?? fetchConfig.baseURL
    // @ts-ignore
    options.initialCache = options.initialCache ?? false
    // @ts-ignore
    options.lazy = options.lazy ?? false
    // @ts-ignore
    options['key'] = new Date().getTime().toString(10)
    // 用户登录，默认传token


    return options
}
//http请求封装
 function useHttp(url:string,options = {}){

     return new Promise(async (resolve,reject) => {

         useFetch(url, {
             ...useGetFetchOptions(options),
             onResponse:({response})=>{
                 if(response._data&&response._data.code===200){
                     console.log("3333")
                     resolve(response._data)
                 }else {
                     reject();
                 }
             }
         })


     })

}

// GET请求
function useHttpGet(url:string,options = {}){
    // @ts-ignore
    options.method = "GET"
    return useHttp(url,options)
}

// POST请求
function useHttpPost(url:string,options = {}){
    // @ts-ignore
    options.method = "POST"
    return useHttp(url,options)
}
export default {
    get:(url:string,params?:any,options={})=>{
        return useHttpGet(url,{query:params,...options})
    },
    post:(url:string,data:any,options:any)=>{
        return useHttpPost(url,{
            body:data,
            ...options
        })
    }
}
